package com.thermofisher.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thermofisher.beans.*;
import com.thermofisher.mapper.*;
import com.thermofisher.service.ProjectService;
import com.thermofisher.utils.DataUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author py
 * @since 2020-01-13
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements
        ProjectService
{

    private static final Logger log = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private EstimateMapper estimateMapper;

    @Autowired
    private ActualAmountMapper actualAmountMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ChanceInfoMapper chanceInfoMapper;

    @Autowired
    private ProjectProductMapper projectProductMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DataUtil dataUtil;

    @Override
    public ProjectData getProjectList(Argument argument)
    {
        argument.setEmail(dataUtil.getEmail(argument.getEmail()));
        Integer pageSize = argument.getPageSize();
        Integer currentPage = argument.getCurrentPage();
        if (null == pageSize || 0 == pageSize)
        {
            pageSize = 10;
        }
        if (null == currentPage || 0 == currentPage)
        {
            currentPage = 1;
        }
        int start = (currentPage - 1) * pageSize;
        int end = currentPage * pageSize;
        String allIds = getAllSubIds(argument.getEmail());
        argument.setEmail(allIds);
        List<Project> list = projectMapper.getProjectList(argument);
        for (Project project : list)
        {
            if (StringUtils.isBlank(project.getLeaderDivision()))
            {
                continue;
            }
            List<Estimate> estimateList = project.getEstimateList();
            Iterator<Estimate> it = estimateList.iterator();
            while (it.hasNext())
            {
                Estimate estimate = it.next();
                if (project.getLeaderDivision().equalsIgnoreCase(estimate.getDepartmentName()))
                {
                    it.remove();
                }
            }
        }
        log.info("==list size==" + list.size());
        if (list.size() < end)
        {
            end = list.size();
        }
        List<Project> subList = list.subList(start, end);
        int projectTotal = list.size();
        ProjectData projectData = new ProjectData(subList, projectTotal);
        return projectData;
    }

    @Override
    public ProjectDetails getProjectDetails(String projectNo, String userId)
    {
        if (StringUtils.isBlank(projectNo))
        {
            log.info("====the method getProjectDetails argument projectNo is null=====");
            return null;
        }
        if (StringUtils.isBlank(userId))
        {
            userId = null;
        }
        Project project = projectMapper.getProjectDetails(projectNo, userId);
        Customer customer = customerMapper.getProjectCustomer(projectNo);
        //List<Estimate> estimateList = estimateMapper.getProjectEstimate(projectNo);
        List<ActualAmount> actualAmountList = actualAmountMapper.getProjectActualAmount(projectNo);
        List<User> userList =
                userMapper.selectList(Wrappers.<User>lambdaQuery().eq(User::getProjectNo,
                                                                      projectNo));
        List<DataDictionary> projectTypeList = projectMapper.getCompanySupport(projectNo);
        List<ProjectProduct> projectProductList =
                projectProductMapper.selectList(
                        Wrappers.<ProjectProduct>lambdaQuery()
                                .eq(ProjectProduct::getProjectNo, projectNo));
        List<ChanceInfo> chanceInfoList =
                chanceInfoMapper.selectList(
                        Wrappers.<ChanceInfo>lambdaQuery().eq(ChanceInfo::getProjectNo, projectNo));
        ProjectDetails projectDetails = new ProjectDetails();
        projectDetails.setProject(project);
        projectDetails.setCustomer(customer);
        //projectDetails.setEstimateList(estimateList);
        //projectDetails.setActualAmountList(actualAmountList);
        projectDetails.setUserList(userList);
        projectDetails.setProjectTypeList(projectTypeList);
        projectDetails.setProjectProductList(projectProductList);
        projectDetails.setChanceInfoList(chanceInfoList);
        return projectDetails;
    }

    public String getAllSubIds(String email)
    {
        if (StringUtils.isBlank(email))
        {
            return null;
        }
        StringBuilder strIds = new StringBuilder();
        List<Employee> employeeList = employeeMapper.getEmployeeByEmail(email);
        if (employeeList.size() > 0)
        {
            strIds.append(employeeList.get(0).getEmployeeNumber());
            String subIds = employeeMapper.getAllSubIds(employeeList.get(0).getEmployeeNumber());
            if (StringUtils.isNotBlank(subIds))
            {
                strIds.append(",").append(subIds);
            }
        }
        return strIds.toString();
    }

    @Override
    public ProjectFilter getProjectFilter()
    {
        ProjectFilter projectFilter = new ProjectFilter();
        List<DataDictionary> projectTypeList = projectMapper.getProjectType();
        List<DataDictionary> supportTypeList = projectMapper.getSupportType();
        List<DataDictionary> projectStatueList = projectMapper.getProjectStatue();
        projectFilter.setProjectTypeList(projectTypeList);
        projectFilter.setSupportTypeList(supportTypeList);
        projectFilter.setProjectStatueList(projectStatueList);
        return projectFilter;
    }

    @Override
    public ProjectFilter getProjectSubFilter(String id)
    {
        ProjectFilter projectFilter = new ProjectFilter();
        List<DataDictionary> projectTypeList = projectMapper.getProjectSubFilter(id);
        projectFilter.setProjectTypeList(projectTypeList);
        return projectFilter;
    }

    @Override
    public ProjectData getProjectWithFilter(SearchProject searchProject)
    {
        searchProject.setEmail(dataUtil.getEmail(searchProject.getEmail()));
        Integer pageSize = searchProject.getPageSize();
        Integer currentPage = searchProject.getCurrentPage();
        if (null == pageSize || 0 == pageSize)
        {
            pageSize = 10;
        }
        if (null == currentPage || 0 == currentPage)
        {
            currentPage = 1;
        }
        int start = (currentPage - 1) * pageSize;
        int end = currentPage * pageSize;
        String allIds = getAllSubIds(searchProject.getEmail());
        searchProject.setEmail(allIds);
        searchProject = dataUtil.getSearchProject(searchProject);
        if (StringUtils.isNotBlank(searchProject.getProjectName()) &&
                StringUtils.isNotBlank(searchProject.getProjectNo()))
        {
            searchProject.setFullQuery(searchProject.getProjectName());
            searchProject.setProjectName("");
            searchProject.setProjectNo("");
            searchProject.setProjectUser("");
            searchProject.setModel("");
            searchProject.setProjectDescription("");
        }
        List<Project> projects = projectMapper.getProjectWithFilter(searchProject);
        for (Project project : projects)
        {
            if (StringUtils.isBlank(project.getLeaderDivision()))
            {
                continue;
            }
            List<Estimate> estimateList = project.getEstimateList();
            Iterator<Estimate> it = estimateList.iterator();
            while (it.hasNext())
            {
                Estimate estimate = it.next();
                if (project.getLeaderDivision().equalsIgnoreCase(estimate.getDepartmentName()))
                {
                    it.remove();
                }
            }
        }
        if (projects.size() < end)
        {
            end = projects.size();
        }
        List<Project> subList = projects.subList(start, end);
        int projectTotal = projects.size();
        ProjectData projectData = new ProjectData(subList, projectTotal);
        return projectData;
    }

    @Override
    public ProjectDetails getProjectInfo(String projectNo, String userId)
    {
        if (StringUtils.isBlank(projectNo))
        {
            log.info("====the method getProjectDetails argument projectNo is null=====");
            return null;
        }
        if (StringUtils.isBlank(userId))
        {
            userId = null;
        }
        Project project = projectMapper.getProjectById(projectNo);
        Customer customer = customerMapper.getProjectCustomer(projectNo);
        ProjectDetails projectDetails = new ProjectDetails();
        projectDetails.setProject(project);
        projectDetails.setCustomer(customer);
        return projectDetails;
    }

    @Override
    public ProjectDetails getProjectMember(String projectNo, String userId)
    {
        if (StringUtils.isBlank(projectNo))
        {
            log.info("====the method getProjectDetails argument projectNo is null=====");
            return null;
        }
        if (StringUtils.isBlank(userId))
        {
            userId = null;
        }
        Project project = projectMapper.getProjectById(projectNo);
        List<User> userList = userMapper.getUserByProjectNo(projectNo);
        ProjectDetails projectDetails = new ProjectDetails();
        projectDetails.setProject(project);
        projectDetails.setUserList(userList);
        return projectDetails;
    }

    @Override
    public ProjectDetails getProjectAmount(String projectNo, String userId)
    {
        if (StringUtils.isBlank(projectNo))
        {
            log.info("====the method getProjectDetails argument projectNo is null=====");
            return null;
        }
        if (StringUtils.isBlank(userId))
        {
            userId = null;
        }
        Project project = projectMapper.getProjectById(projectNo);
        String business = estimateMapper.getBusiness(projectNo);
        project.setProjectSource(business);
        List<Estimate> estimateList = estimateMapper.getProjectEst(projectNo);
        List<Estimate> actualList = estimateMapper.getProjectAct(projectNo);
        ProjectDetails projectDetails = new ProjectDetails();
        projectDetails.setProject(project);
        projectDetails.setEstimateList(estimateList);
        projectDetails.setActualAmountList(actualList);
        return projectDetails;
    }

    @Override
    public ProjectDetails getProjectProduct(String projectNo, String userId)
    {
        if (StringUtils.isBlank(projectNo))
        {
            log.info("====the method getProjectDetails argument projectNo is null=====");
            return null;
        }
        if (StringUtils.isBlank(userId))
        {
            userId = null;
        }
        List<ProjectProduct> projectProductList = projectProductMapper.selectProductList(projectNo);
        List<ChanceInfo> chanceInfoList = chanceInfoMapper.getChanceInfoList(projectNo);
        ProjectDetails projectDetails = new ProjectDetails();
        projectDetails.setProjectProductList(projectProductList);
        projectDetails.setChanceInfoList(chanceInfoList);
        return projectDetails;
    }

}
